package Services

import(
	"github.com/gin-gonic/gin"
	"fmt"
	"net/http"
	"gin/Databases"
	"gin/Middlewares"
	"strconv"
	"gin/Common"
)


func confirmAttrData(id int, relation_id int) bool {
	tx, err1 := Databases.Db.Begin()
	if err1 != nil {
		if tx != nil {
			_ = tx.Rollback()
		}
		fmt.Printf("begin trans action failed, err:%v\n", err1)
		return false
	}

	var sql_insert = ""
	if (relation_id == Common.AttrV3_tiaojia) {
		sql_insert = "insert into relation_data_03_tiaojia (Fsrc_code, Fsrc_type, Ftitle, Fdate, Furl, Fsite) select Fsrc_code, Fsrc_type, Ftitle, Fdate, Furl, Fsite from relation_data_03_temp where Fid = ?;"
	} else if (relation_id == Common.AttrV3_chufa) {
		sql_insert = "insert into relation_data_03_chufa (Fsrc_code, Fsrc_type, Ftitle, Fdate, Furl, Fsite) select Fsrc_code, Fsrc_type, Ftitle, Fdate, Furl, Fsite from relation_data_03_temp where Fid = ?;"
	} else if (relation_id == Common.AttrV3_susong) {
		sql_insert = "insert into relation_data_03_susong (Fsrc_code, Fsrc_type, Ftitle, Fdate, Furl, Fsite) select Fsrc_code, Fsrc_type, Ftitle, Fdate, Furl, Fsite from relation_data_03_temp where Fid = ?;"
	} else if (relation_id == Common.AttrV3_diaoyan) {
                sql_insert = "insert into relation_data_03_diaoyan (Fsrc_code, Fsrc_type, Ftitle, Fdate, Furl, Fsite) select Fsrc_code, Fsrc_type, Ftitle, Fdate, Furl, Fsite from relation_data_03_temp where Fid = ?;"
        } else if (relation_id == Common.AttrV3_chanyelian) {
                sql_insert = "insert into relation_data_03_chanyelian (Fsrc_code, Fsrc_type, Ftitle, Fdate, Furl, Fsite) select Fsrc_code, Fsrc_type, Ftitle, Fdate, Furl, Fsite from relation_data_03_temp where Fid = ?;"
        } else if (relation_id == Common.AttrV3_guanli) {
                sql_insert = "insert into relation_data_03_guanli (Fsrc_code, Fsrc_type, Ftitle, Fdate, Furl, Fsite) select Fsrc_code, Fsrc_type, Ftitle, Fdate, Furl, Fsite from relation_data_03_temp where Fid = ?;"
        } else if (relation_id == Common.AttrV3_yeji) {
                sql_insert = "insert into relation_data_03_yeji (Fsrc_code, Fsrc_type, Ftitle, Fdate, Furl, Fsite) select Fsrc_code, Fsrc_type, Ftitle, Fdate, Furl, Fsite from relation_data_03_temp where Fid = ?;"
        } else if (relation_id == Common.AttrV3_other) {
		sql_insert = "insert into relation_data_03_other (Fsrc_code, Fsrc_type, Ftitle, Fdate, Furl, Fsite) select Fsrc_code, Fsrc_type, Ftitle, Fdate, Furl, Fsite from relation_data_03_temp where Fid = ?;"
	} else {
		_ = tx.Rollback()
		return false
	}

	_,err2 := Databases.Db.Exec(sql_insert, id)
	if err2 != nil {
		_ = tx.Rollback()
		fmt.Println("delete failed, ", err2)
		return false
	}

	update_sql := "update relation_data_03_temp set Fid=Fid + 20000000,Fstatus=1,Fmodify_time=now() where Fid=?;"
	_,err3 := Databases.Db.Exec(update_sql, id)
	if err3 != nil {
		_ = tx.Rollback()
		fmt.Println("delete failed, ", err3)
		return false
	}
	tx.Commit()
	return true
}


// 宏观数据
func AttrDataConfirm(c *gin.Context) {
	if (!Middlewares.CheckLogin(c)) {
		c.JSON(http.StatusOK, gin.H{
	        "result": -1,
	        "msg":    "login fail",
	        "data": "{}",
	    })
	    return
	}

	id, _ := strconv.Atoi(c.Query("id"))
	relation_id,_ := strconv.Atoi(c.Query("attr_type"))

	if (!confirmAttrData(id, relation_id)) {
		c.JSON(http.StatusOK, gin.H{
	        "result": -2,
	        "msg":    "confirm fail",
	        "data": "{}",
	    })
	    return
	}

    c.JSON(http.StatusOK, gin.H{
        "result": 0,
        "msg":    "success",
        "data": "",
    })
}

